// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include <QException>
#include <QtConcurrent>

namespace FirstMyException
{
    //! [0]
    class MyException : public QException
    {
    public:
        void raise() const override { throw *this; }
        MyException *clone() const override { return new MyException(*this); }
    };
    //! [0]
}

class MyException : public QException
{
public:
    void raise() const override;
    MyException *clone() const override;
};

void throwFunction(int num)
{
    throw MyException();
}

void example(QList <int> &list)
{
    //! [1]
    try  {
        QtConcurrent::blockingMap(list, throwFunction); // throwFunction throws MyException
    } catch (MyException &e) {
        // handle exception
    }
    //! [1]
}

//! [2]
void MyException::raise() const { throw *this; }
//! [2]


//! [3]
MyException *MyException::clone() const { return new MyException(*this); }
//! [3]

void wrapInFunction()
{
    //! [4]
    try {
        auto f = QtConcurrent::run([] { throw MyException {}; });
        // ...
    } catch (const QUnhandledException &e) {
        try {
            if (e.exception())
                std::rethrow_exception(e.exception());
        } catch (const MyException &ex) {
            // Process 'ex'
        }
    }
    //! [4]
}
